﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class DialogWorker : Form
    {
        private TextBox _workerId = null;
        private TextBox _workerName = null;
        private List<WORKER> _listWorker = null;

        public DialogWorker(TextBox id,TextBox name=null)
        {
            _workerId = id;
            _workerName = name;
            InitializeComponent();
        }
        /// <summary>
        /// 写信息到List view中
        /// </summary>
        /// <param name="view"></param>
        /// <param name="worker"></param>
        private void SetListViewWork(ListView view, WORKER worker)
        {
            if (view == null || worker == null)
            {
                return;
            }

            ListViewItem item = new ListViewItem();
            item.Text = worker.ID.ToString();
            item.SubItems.Add(worker.NAME);
            item.SubItems.Add(worker.SEX);
            if (worker.ENTER_DATE != null)
                item.SubItems.Add(worker.ENTER_DATE.ToString());
            else
                item.SubItems.Add("");
            item.SubItems.Add(worker.CARDID);

            view.Items.Add(item);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            string name = this.textBoxName.Text.Trim();

            WORKER workerW = new WORKER();
            if (string.IsNullOrEmpty(name) == false)//不输入姓名查询所有人
            {
                workerW.SetWhere();
                workerW.NAME = name;
            }

            var selectWorker = MethodWcf.SelectWorkerClient(workerW);
            if (selectWorker.result)
            {
                _listWorker = selectWorker.value;
                for(int i = 0; i < _listWorker.Count; i++)
                {
                    SetListViewWork(this.listViewWorker, _listWorker[i]);
                }
            }
            else
            {
                MessageBox.Show(string.Format("未能查询到相关信息，{0}",selectWorker.message));
            }
        }

        private void listViewWorker_MouseClick(object sender, MouseEventArgs e)
        {
            if (_listWorker.Count == 1)//只有一个人
            {
                _workerId.Text = _listWorker[0].ID.ToString();
                _workerName.Text = _listWorker[0].NAME;
                Close();
                return;
            }

            var choice = (from w in _listWorker
                          where w.ID == long.Parse(this.listViewWorker.SelectedItems[0].Text)
                          select w).FirstOrDefault();

            if (choice == null)
            {
                MessageBox.Show("没有找到相应的人员信息");
                Close();
                return;
            }

            _workerId.Text = choice.ID.ToString();
            _workerName.Text = choice.NAME;
            Close();
        }
    }
}
